import xml.dom.minidom
import random
import os, sys

'''
class IssueQuestion:
    def __init__(self, title, descript, id=0):
		self.title = title
		self.descript = descript
		self.id=id
	
def read_issue_xml():	
	doc = xml.dom.minidom.parse("issue.xml")

	i_nodes=doc.getElementsByTagName('subject')
	IssueArr=[]
	for i_node in i_nodes:
		id = int(i_node.getAttribute('id'))
		Tstr=""
		Dstr=""
		i_nodes_T = i_node.getElementsByTagName('Title')
		for i_node_T in i_nodes_T:
			for i_node_Tc in i_node_T.childNodes:
	#			print(i_node_Tc.data)
				Tstr=i_node_Tc.data

		i_nodes_D = i_node.getElementsByTagName('Descript')
		for i_node_D in i_nodes_D:
			for i_node_Dc in i_node_D.childNodes:
	#			print(i_node_Dc.data)
				Dstr=i_node_Dc.data
				
		IssueArr.append(IssueQuestion(Tstr,Dstr,id))
	
	return IssueArr
'''
class ArgueQuestion:
    def __init__(self, title, descript, id=0):
		self.title = title
		self.descript = descript
		self.id=id
		
def read_raw_argume():
#	doc = xml.dom.minidom.parse("argue_raw_tiny.html")
	doc = xml.dom.minidom.parse("argue_raw.html")
	html_note=doc.firstChild
	ArgueArr=[]
	pnodes=html_note.getElementsByTagName('p')
	id = 0
	Tstr=""
	Dstr=""
	for pnode in pnodes:
#		print(pnode.getAttribute('class'))
		if('descript'==pnode.getAttribute('class')):
			Dstr=pnode.firstChild.data
			
			
			print('Title:   '+Tstr)
			print('Descript:   '+Dstr)
			print('Id:   '+str(id))
			print('*'*10)
			
			
			
			ArgueArr.append(ArgueQuestion(Tstr,Dstr,id))
			id = id+1
			Tstr=""
		else:
			Tstr+=pnode.firstChild.data
		
	return ArgueArr
	
def write_argume(Arr):
	doc=xml.dom.minidom.Document()
	# Create the <issue> base element
	issue = doc.createElement("argue")
	doc.appendChild(issue)

	index_no = 0
	
	for arg in Arr:
		subject = doc.createElement("subject")
		subject.setAttribute("id", str(index_no))
		issue.appendChild(subject)
	
		# Create a <Title> element
		title_text = doc.createElement("Title")
		subject.appendChild(title_text)
		# Give the <Title> elemenet the text
		t_text = doc.createTextNode(arg.title)
		title_text.appendChild(t_text)

		# Create a <Descript> element
		descript_text = doc.createElement("Descript")
		subject.appendChild(descript_text)
		# Give the <Descript> elemenet the text
		p_text = doc.createTextNode(arg.descript)
		descript_text.appendChild(p_text)
		
		index_no = index_no + 1
	
	result = doc.toprettyxml(indent="  ")
#	file = open('argue_tiny.xml', mode='w')
	file = open('argue.xml', mode='w')
	file.write(result)
	file.close()
	
#Test read raw argue xml
Arr = read_raw_argume()
print(str(len(Arr)))
write_argume(Arr)

# Test read issue xml 	
'''Arr = read_issue_xml()
len = len(Arr)
r_id = random.randrange(-1,len)
r_is=Arr[r_id]
print(r_is.id)
print(r_is.title)
print(r_is.descript)'''



